// const HTTP = "https://reproduce.ascetic.top/api/";
const HTTP = "https://www.manmiao.online/api/";

import st from "@/utils/showtoast.js";
import stoarge from "@/utils/stoarge.js";

export const get = (url, data = {}) => {
	return request(url, "GET", data);
};

export const post = (url, data = {}) => {
	return request(url, "POST", data);
};

/**
 * 网络请求
 * @param {String} url 请求地址
 * @param {'GET'|'POST'} method 请求方式
 * @param {Object} data 请求参数
 */
const request = (url, method, data) => {
	uni.showLoading({
		title: "请求中",
		mask: true,
	});
	return new Promise((resolve, reject) => {
		let token = stoarge.getToken();
		uni.request({
			url: HTTP + url,
			method,
			data,
			header: {
				"Content-Type": "application/json",
				token,
			},
			success: (res) => {
				uni.hideLoading();
				uni.stopPullDownRefresh();
				let model = res.data;
				// console.log("网络请求的所有数据", data, url, token, res);
				if (model.code === 1) {
					resolve(model);
				} else {
					reject(model);
				}
			},
			fail: (res) => {
				uni.hideLoading();
				uni.stopPullDownRefresh();
				st.show("网络请求失败，请检查网络连接或稍后重试");
			},
		});
	});
};

export default {
	get,
	post,
	HTTP
};
